package web.dmt.dao.impl;
import java.sql.*;
import web.dmt.dao.IDAO;
import web.dmt.vo.Myuser;

public class Iimpl implements IDAO{
 private Connection conn=null; 
 private PreparedStatement pstm=null;
 public Iimpl(Connection conn){
	 this.conn=conn;
 }
 public boolean doGreate(Myuser users) throws Exception {
		 boolean flag=false;
		    String sql="insert into users(uemail,upassword,uname,usex,uage,ucareer,uschool,uinfo,usafe,uanswer,udate) values(?,?,?,?,?,?,?,?,?,?,?)";
		    this.pstm=this.conn.prepareStatement(sql);
		    this.pstm.setString(1,users.getUemail());
		    this.pstm.setString(2,users.getUpassword());
		    this.pstm.setString(3,users.getUname());
		    this.pstm.setString(4,users.getUsex());
		    this.pstm.setInt(5,users.getUage());
		    this.pstm.setString(6,users.getUcareer());
		    this.pstm.setString(7,users.getUschool());
		    this.pstm.setString(8,users.getUinfo());
		    this.pstm.setString(9,users.getUsafe());
		    this.pstm.setString(10,users.getUanswer());
		    this.pstm.setTimestamp(11, new java.sql.Timestamp(new java.util.Date().getTime()));		    
		    if(this.pstm.executeUpdate()>0){
		    	flag=true;
		    }
		return flag;
	}
 public Myuser findLogin(String uemail, String upassword) throws Exception {
		Myuser user=null;
		String sql="SELECT uid,uemail,upassword,uname,usex,uage,uinterest,ucareer,uschool,uinfo,ufiled,ucid,usafe,uanswer FROM users WHERE uemail=? AND upassword=?";
		 this.pstm=this.conn.prepareStatement(sql);
		 this.pstm.setString(1,uemail);
		 this.pstm.setString(2,upassword);
		 ResultSet rs=this.pstm.executeQuery();
		 while(rs.next()){
			 user=new Myuser();
			 user.setUid(rs.getInt(1));
			 user.setUemail(rs.getString(2));
			 user.setUpassword(rs.getString(3));
			 user.setUname(rs.getString(4));
			 user.setUsex(rs.getString(5));
			 user.setUage(rs.getInt(6));
			 user.setUinterest(rs.getString(7));
			 user.setUcareer(rs.getString(8));
			 user.setUschool(rs.getString(9));
			 user.setUinfo(rs.getString(10));
			 user.setUcid(rs.getInt(11));
			 user.setUsafe(rs.getString(12));
			 user.setUanswer(rs.getString(13));
		 }
		return user;
	}
 public Myuser findById(int Id)throws Exception{
	 Myuser user=null;
	 String sql="select uid,uemail,upassword,uname,usex,uage,career,uschool,uinfo,usafe,uanswer from users where uemail=?";
	 this.pstm=this.conn.prepareStatement(sql);
	 this.pstm.setInt(1, Id);
	 ResultSet rs=this.pstm.executeQuery();
	 while(rs.next()){
		 user=new Myuser();
		 user.setUid(rs.getInt(1));
		 user.setUemail(rs.getString(2));
		 user.setUpassword(rs.getString(3));
		 user.setUname(rs.getString(4));
		 user.setUsex(rs.getString(5));
		 user.setUage(rs.getInt(6));
		 user.setUcareer(rs.getString(7));
		 user.setUschool(rs.getString(8));
		 user.setUinfo(rs.getString(9));
		 user.setUsafe(rs.getString(10));
		 user.setUanswer(rs.getString(11));
	 }
	 return user;
 }
 public boolean findLogin(Myuser users)throws Exception{
	 boolean flag=false;
	 String sql="SELECT uid FROM users WHERE uemail=? AND upassword=?";
	 this.pstm=this.conn.prepareStatement(sql);
	 this.pstm.setString(1,users.getUemail());
	 this.pstm.setString(2,users.getUpassword());
	 ResultSet rs=this.pstm.executeQuery();
	 while(rs.next()){
		 users.setUname(rs.getString(1));
		 flag=true;
	 }
	 this.pstm.close();
	 return flag;
 }
public boolean doUpdate(Myuser user) throws Exception {
	boolean flag=false;
	String sql="update users set uemail=?,upassword=?,uname=?,usex=?,uage=?,uinterest=?,ucareer=?,uschool=?,upoto=?,uinfo=?,ufiled=?,ucid=?,usafe=?,usafe=?,uanswer=?";
	 this.pstm=this.conn.prepareStatement(sql);
	 this.pstm.setString(1,user.getUemail());
	 this.pstm.setString(2,user.getUpassword());
	 this.pstm.setString(3,user.getUname());
	 this.pstm.setString(4, user.getUsex());
	 this.pstm.setInt(5,user.getUage());
	 this.pstm.setString(6,user.getUinterest());
	 this.pstm.setString(7,user.getUcareer());
	 this.pstm.setString(8,user.getUschool());
	 this.pstm.setString(9, user.getUphoto());
	 this.pstm.setString(10,user.getUinfo());
	 this.pstm.setString(11,user.getUfiled());
	 this.pstm.setInt(12,user.getUcid());
	 this.pstm.setString(13,user.getUsafe());
	 this.pstm.setString(14,user.getUanswer());
	 if(this.pstm.executeUpdate()>0){
		 flag=true;
	 }
	return flag;
}


}
